<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0">班级管理</h5>
            <button class="btn btn-primary" onclick="openClassModal()">
                <i class="fas fa-plus"></i> 添加班级
            </button>
        </div>
        <div class="card-body">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>班级名称</th>
                        <th>年级</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="class : ${classes}">
                        <td th:text="${class.id}"></td>
                        <td th:text="${class.className}"></td>
                        <td th:text="${class.gradeName}"></td>
                        <td>
                            <button class="btn btn-sm btn-warning btn-action" th:onclick="'editClass(' + ${class.id} + ')'">
                                <i class="fas fa-edit"></i>
                            </button>
                            <button class="btn btn-sm btn-danger btn-action" th:onclick="'deleteClass(' + ${class.id} + ')'">
                                <i class="fas fa-trash"></i>
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <!-- 添加/编辑班级模态框 -->
    <div class="modal fade" id="classModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">班级信息</h5>
                    <button type="button" class="close" data-dismiss="modal">
                        <span>&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="classForm">
                        <input type="hidden" id="classId">
                        <div class="form-group">
                            <label>班级名称</label>
                            <input type="text" class="form-control" id="className" required>
                        </div>
                        <div class="form-group">
                            <label>年级</label>
                            <select class="form-control" id="gradeId" required>
                                <option value="">请选择年级</option>
                                <option th:each="grade : ${grades}" 
                                        th:value="${grade.id}" 
                                        th:text="${grade.gradeName}">
                                </option>
                            </select>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="saveClass()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        function openClassModal(id) {
            if (id) {
                $.get('/class/' + id, function(data) {
                    $('#classId').val(data.id);
                    $('#className').val(data.className);
                    $('#gradeId').val(data.gradeId);
                    $('#classModal').modal('show');
                });
            } else {
                $('#classForm')[0].reset();
                $('#classId').val('');
                $('#classModal').modal('show');
            }
        }

        function saveClass() {
            var classData = {
                id: $('#classId').val(),
                className: $('#className').val(),
                gradeId: $('#gradeId').val()
            };

            var url = classData.id ? '/class' : '/class';
            var method = classData.id ? 'PUT' : 'POST';

            $.ajax({
                url: url,
                type: method,
                contentType: 'application/json',
                data: JSON.stringify(classData),
                success: function() {
                    $('#classModal').modal('hide');
                    loadContent('class');
                }
            });
        }

        function deleteClass(id) {
            if(confirm('确定要删除这个班级吗？')) {
                $.ajax({
                    url: '/class/' + id,
                    type: 'DELETE',
                    success: function() {
                        loadContent('class');
                    }
                });
            }
        }
    </script>
</body>
</html> 